package org.dsc.amafinder;

import org.dsc.amafinder.control.AmfST;
import org.dsc.amafinder.control.af.AmfQR;
import org.dsc.amafinder.view.AInfo;
import org.dsc.amafinder.view.AWebView;
import org.dsc.amafinder.view.DlgCloseApp;
import org.dsc.sdk.DSC;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class ASearch extends Activity implements
RadioGroup.OnCheckedChangeListener, View.OnClickListener {
   private EditText descuentoToSearch;
   private AutoCompleteTextView articuloToSearch;
   private Button btnSearch;
   private String paisBusqueda;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.asearch);

      paisBusqueda="es";

		RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
		mRadioGroup.setOnCheckedChangeListener(this);
		
      descuentoToSearch = (EditText) findViewById(R.id.descuentoToSearch);
      // get a reference to the AutoCompleteTextView in the layout
      articuloToSearch = (AutoCompleteTextView) findViewById(R.id.articuloToSearch);
      // get the string array
      String[] articulos = AmfST.toArray();
      // create the adapter and set it to the AutoCompleteTextView
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, articulos);
      articuloToSearch.setAdapter(adapter);
      articuloToSearch.setOnEditorActionListener(new OnEditorActionListener() {
         @Override
         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
               InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
               inputManager.hideSoftInputFromWindow(articuloToSearch.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
               btnSearch.requestFocus();
               handled = true;
            }
            return handled;
         }
      });

      btnSearch = (Button) findViewById(R.id.btnSearch);
      btnSearch.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
        	 String url =AmfQR.formatSearchUrl(descuentoToSearch.getText().toString(), articuloToSearch.getText().toString(), paisBusqueda);
        	 lanzar(url, paisBusqueda);
         }
      });
      // poner adView
      DSC.A(this).createAdv(R.id.PubliLayoutSearch);
   }

   @Override
   protected void onResume() {
      super.onResume();
      DSC.A(this).resume();
   }

   @Override
   protected void onRestart() {
      super.onRestart();
      DSC.A(this).restart();
   }

   @Override
   public void onPause() {
      DSC.A(this).pause();
      super.onPause();
   }

   @Override
   public void onDestroy() {
      DSC.A(this).destroy();
      super.onDestroy();
   }

   // Lo vuelvo a poner aqui ya que no estaban funcionando correctamente las
   // clases que deberia pintar si se quiere salir o no
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
         DlgCloseApp.mostrar(this);
      }
      // para las demas cosas, se reenvia el evento al listener habitual
      return super.onKeyDown(keyCode, event);
   }
   
   protected void lanzar(String url, String pais) {
		Intent AWebViewIntent = new Intent().setClass(this, AWebView.class);
		AWebViewIntent.putExtra("URL", url);
		AWebViewIntent.putExtra("PAISBUSQUEDA", pais);
		startActivity(AWebViewIntent);
	}

   /*
    * @ini-menu-action-bar
    */
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu items for use in the action bar
      getMenuInflater().inflate(R.menu.main_activity_actions, menu);
      menu.findItem(R.id.action_search).setIntent(new Intent(this, ASearch.class));
      menu.findItem(R.id.action_info).setIntent(new Intent(this, AInfo.class));
      // menu.findItem(R.id.action_favorites).setIntent(new Intent(this, AFavorites.class));
      return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.action_search:
         descuentoToSearch.setText("");
         articuloToSearch.setText("");
         break;
      case R.id.action_info:
         // case R.id.action_favorites:
         startActivity(item.getIntent());
         break;
      default:
         return super.onOptionsItemSelected(item);
      }
      return true;
   }
   /*
    * @end-menu-action-bar
    */
   
   @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (checkedId == R.id.radioButtonES) {
			paisBusqueda = "es";
		}
		if (checkedId == R.id.radioButtonFR) {
			paisBusqueda = "fr";
		}
		if(checkedId == R.id.radioButtonDE){
	        paisBusqueda="de";
	   }
		if(checkedId == R.id.radioButtonIT){
	        paisBusqueda="it";
	   }
		if(checkedId == R.id.radioButtonUK){
	        paisBusqueda="co.uk";
	   }
	}
}
